#!/bin/sh
# requires: wget, date, cut, html2text, sed

# change this to the bus of interest
BUSSTOP=${1:-"50377"}

# some URL variables that are time dependent
HOUR=$(date +%I00)
MINUTE=$(( 10#$(date +%M) / 5 * 5 ))
DATE=$(date +%P | cut -b1)

# unique string to demarcate the top of the table of interest
# this changes when the webpage/website is changed
TABLE_TOP=$(date +"Date %m-%d-%Y")

# The final assembled URL 
NB_URL="http://tripplanning.translink.bc.ca/hiwire?.a=iNextBusFind&.s=407ae141&ShowTimes=1&NumStopTimes=19&LineDirId=&GetSchedules=1&Geocode=0&FormState=0&StopAbbr=${BUSSTOP}&FromHourDropDown=${HOUR}&FromMinuteDropDown=${MINUTE}&FromMeridiemDropDown=${DATE}&SB=Search&.a=iTripPlanning"

# get the html page, convert to text, then strip away all but bus times
# there are MANY ways to do this. [see striptable.txt for more examples]
wget -O - $NB_URL  | 
  html2text | 
# logic: the range of interest is from the top of the table and the next 
#+ 19 lines. So _don't_ delete these lines (!d). Also remove the leading 
#+ spaces.
   sed '/${TABLE_TOP}/,+19!d ; s/^ *//' |
  less

exit 0
